package com.yict.csms.system.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import com.yict.common.dao.impl.CommonDao;
import com.yict.csms.system.entity.UserUserGroup;

/**
 * 
 * 
 * @author Patrick.Deng
 * 
 */
@Repository
public class UserUserGroupDaoImpl extends CommonDao<UserUserGroup, Long> {

	public UserUserGroup findGroup(Long userId, Long userGroupId) {
		String queryString = " from UserUserGroup as u where u.user.userId =:userId and u.userGroup.userGroupId = :userGroupId";
		UserUserGroup en = new UserUserGroup();
		try {
			Query query = this.getSession().createQuery(queryString);
			query.setParameter("userId", userId);
			query.setParameter("userGroupId", userGroupId);
			List<UserUserGroup> list = query.list();
			if (list != null && list.size() > 0) {
				en = list.get(0);
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
		return en;
	}

	public List<UserUserGroup> getCruNameList(Long userId) {
		List<UserUserGroup> list = new ArrayList<UserUserGroup>();
		String queryString = "from UserUserGroup u where u.user.userId =:userId order by u.createDate desc";
		try {
			Query query = this.getSession().createQuery(queryString);
			query.setLong("userId", userId);
			list = query.list();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

	public boolean check(Long userGroupId) {
		String queryString = " from UserUserGroup as u where u.userGroup.userGroupId = :userGroupId";
		try {
			Query query = this.getSession().createQuery(queryString);
			query.setParameter("userGroupId", userGroupId);
			List<UserUserGroup> list = query.list();
			if (list != null && list.size() > 0) {
				return true;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}
}
